## X2D Game Engine
## Copyright (C) 2006, 2007, 2008 Janusz Kowalski
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Minimum required CMake version.
# CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)

# Automatically escape preprocessor definition values.
IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 2.4)
  CMAKE_POLICY(SET CMP0005 NEW)
  SET(QUOT "")
ELSE(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 2.4)
  SET(QUOT "'")
ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 2.4)

PROJECT(x2d)

OPTION(X2D_DEBUG "Add the debug code." OFF)

IF(X2D_DEBUG)
  ADD_DEFINITIONS(-DDEBUG)
ENDIF(X2D_DEBUG)

# Test for OpenGl support.
FIND_PACKAGE(OpenGL)
IF(OPENGL_FOUND)
  OPTION(X2D_WITH_OPENGL "Use OpenGL." ON)
ENDIF(OPENGL_FOUND)

IF(X2D_WITH_OPENGL)
  ADD_DEFINITIONS(-DX2D_WITH_OPENGL)
  INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
ENDIF(X2D_WITH_OPENGL)

FIND_PACKAGE(SDL REQUIRED)
INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR})

FIND_PACKAGE(SDL_mixer REQUIRED)
INCLUDE_DIRECTORIES(${SDLMIXER_INCLUDE_DIR})

FIND_PACKAGE(PNG REQUIRED)
INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})

FIND_PACKAGE(ZLIB REQUIRED)
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})

# Use Boost library.
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREAD OFF)

FIND_PACKAGE(Boost 1.36.0 COMPONENTS filesystem REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

# Add Lua
# !! Shouldn't we include directory with header files only?
ADD_SUBDIRECTORY(Lua)
INCLUDE_DIRECTORIES(Lua)
SET(LUA_LIBRARY lua)

# Add Box2D
ADD_SUBDIRECTORY(Box2D)
INCLUDE_DIRECTORIES(Box2D)
SET(BOX2D_LIBRARY box2d)

# Definitions for OS.
IF(WIN32)
  ADD_DEFINITIONS(-DOS_WINDOWS)
ENDIF(WIN32)

IF(UNIX)
  ADD_DEFINITIONS(-DOS_UNIX)
ENDIF(UNIX)

IF(MSVC)
  ADD_DEFINITIONS(-DMSVC)
ENDIF(MSVC)

IF(UNIX)
INSTALL(DIRECTORY data/ DESTINATION share/x2d/data/)
ADD_DEFINITIONS(-DX2D_PATH_DATA=${QUOT}"${CMAKE_INSTALL_PREFIX}/share/x2d/data/"${QUOT})
ENDIF(UNIX)

IF(WIN32)
INSTALL(DIRECTORY data/ DESTINATION data/)
ADD_DEFINITIONS(-DX2D_PATH_DATA="data/")
ENDIF(WIN32)

SET(SOURCES
fontSDL.cpp
imgSDL.cpp
main.cpp
mainLoop.cpp
x2d_Anim.cpp
x2d_BgLayer.cpp
x2d_editMode.cpp
x2d_ext.cpp
x2d_gameMode.cpp
x2d_main.cpp
x2d_MainScreen.cpp
x2d_Map.cpp
x2d_New.cpp
x2d_View.cpp
x2tk.cpp
x2tk_Button.cpp
x2tk_Container.cpp
x2tk_Rectangle.cpp
x2tk_Screen.cpp
x2tk_VPanel.cpp
x2tk_Widget.cpp
x2tk_Window.cpp
x2tk_Graphics.cpp
x2tk_TextField.cpp
x2tk_VText.cpp
pmsg.cpp
SDL_gfxPrimitives.cpp
SDL_gfxPrimitives.h
os.cpp
)

IF(MSVC)
  # Add resource file to sources list.
  SET(SOURCES ${SOURCES} x2d.rc) 
  # Disable some warnings.
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS=1)
ENDIF(MSVC)

# Custom command to compile resource file.
IF(MINGW)
  ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/x2d_rc.o COMMAND windres.exe
  -I${CMAKE_CURRENT_SOURCE_DIR}
  -i${CMAKE_CURRENT_SOURCE_DIR}/x2d.rc
  -o ${CMAKE_CURRENT_BINARY_DIR}/x2d_rc.o
  )
  SET(SOURCES ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/x2d_rc.o)
ENDIF(MINGW)

ADD_EXECUTABLE(x2d ${SOURCES})

SET(LIBS ${SDL_LIBRARY} ${PNG_LIBRARY} ${ZLIB_LIBRARIES} ${SDLMIXER_LIBRARY} ${LUA_LIBRARY}
${BOX2D_LIBRARY})

IF(MSVC)
# Boost libraries to link are deterined automatically.
# Uncomment the line below to link dynamically.
# ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK)
ELSE (MSVC)
  SET(LIBS ${LIBS} boost_filesystem)
ENDIF(MSVC)

IF(X2D_WITH_OPENGL)
  SET(LIBS ${LIBS} ${OPENGL_LIBRARIES})
ENDIF(X2D_WITH_OPENGL)
TARGET_LINK_LIBRARIES(x2d ${LIBS})

IF(WIN32 AND NOT UNIX)
INSTALL(TARGETS x2d RUNTIME DESTINATION bin/)
IF(EXISTS ${CMAKE_SOURCE_DIR}/dlls/)
  INSTALL(DIRECTORY dlls/ DESTINATION bin/)
ENDIF(EXISTS ${CMAKE_SOURCE_DIR}/dlls/)
ELSE(WIN32 AND NOT UNIX)
INSTALL(TARGETS x2d RUNTIME DESTINATION bin/)
ENDIF(WIN32 AND NOT UNIX)

include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_NAME "x2d")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "x2d - simple game engine")
set(CPACK_PACKAGE_VERSION "test")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "x2d")
set(CPACK_PACKAGE_EXECUTABLES "x2d;X2D Game Engine")
set(CPACK_PACKAGE_VENDOR krot)
set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_SOURCE_DIR}/README.txt)
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/COPYING.txt)
set(CPACK_NSIS_MUI_ICON ${CMAKE_SOURCE_DIR}/x2d-setup.ico)
set(CPACK_NSIS_MUI_UNIICON ${CMAKE_SOURCE_DIR}/x2d-setup.ico)
include(CPack)

